package cn.melonlib.apis.http.comp.action;

import cn.melonlib.apis.http.enums.ResponseStatus;
import com.fasterxml.jackson.databind.ObjectMapper;
import okhttp3.*;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Map;

@Component
public class ApiRequestAction {


    @Resource
    private OkHttpClient httpClient;

    @Resource
    private ObjectMapper jsonMapper;

    public ApiResponseBody post(String url, Map<String,Object> body,Map<String,String> headers,String contentType) throws Exception{
        MediaType mediaType=MediaType.parse(contentType);
        RequestBody requestBody=RequestBody.create(jsonMapper.writeValueAsBytes(body),mediaType);
        Request request=new Request.Builder().url(url).post(requestBody).headers(
                headers.entrySet().stream().reduce(new Headers.Builder(),(h, m)->h.add(m.getKey(),m.getValue()),(h, m)->null)
                        .build()).build();
        long startTime=System.currentTimeMillis();
        try(Response response=httpClient.newCall(request).execute()){
            return switch (response.code()){
                case 200->new ApiResponseBody(200,response.body().string(),startTime,System.currentTimeMillis(), ResponseStatus.SUCCESS);
                case 404,403,405,401->new ApiResponseBody(200,response.body().string(),startTime,System.currentTimeMillis(), ResponseStatus.CLIENT_ERROR);
                case 500,501,502,503->new ApiResponseBody(200,response.body().string(),startTime,System.currentTimeMillis(), ResponseStatus.SERVICE_ERROR);
                default -> new ApiResponseBody(response.code(), response.body().string(),startTime,System.currentTimeMillis(), ResponseStatus.UNKOWN_ERROR);
            };
        }catch (Exception e){
            StringWriter sw=new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            return new ApiResponseBody(200,sw.toString(),startTime,System.currentTimeMillis(), ResponseStatus.API_ERROR);
        }
    }
    public ApiResponseBody get(String url,Map<String,String> headers) throws Exception{
        Request request=new Request.Builder().url(url).get().headers(
                headers.entrySet().stream().reduce(new Headers.Builder(),(h, m)->h.add(m.getKey(),m.getValue()),(h, m)->null)
                        .build()).build();
        long startTime=System.currentTimeMillis();
        try(Response response=httpClient.newCall(request).execute()){
            return switch (response.code()){
                case 200->new ApiResponseBody(200,response.body().string(),startTime,System.currentTimeMillis(), ResponseStatus.SUCCESS);
                case 404,403,405,401->new ApiResponseBody(response.code(),response.body().string(),startTime,System.currentTimeMillis(), ResponseStatus.CLIENT_ERROR);
                case 500,501,502,503->new ApiResponseBody(response.code(),response.body().string(),startTime,System.currentTimeMillis(), ResponseStatus.SERVICE_ERROR);
                default -> new ApiResponseBody(response.code(), response.body().string(),startTime,System.currentTimeMillis(), ResponseStatus.UNKOWN_ERROR);
            };
        }catch (Exception e){
            StringWriter sw=new StringWriter();
            e.printStackTrace(new PrintWriter(sw));
            return new ApiResponseBody(200,sw.toString(),startTime,System.currentTimeMillis(), ResponseStatus.API_ERROR);
        }
    }
}
